package com.youlu.campus.service.config.impl;

import com.tencentcloudapi.live.v20180801.models.CallBackRuleInfo;
import com.youlu.campus.entity.OuterAppIpConfig;
import com.youlu.campus.service.config.OuterAppIpConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class OuterAppIpConfigServiceImpl implements OuterAppIpConfigService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<OuterAppIpConfig> findByOuterAppConfigId(String outerAppConfigId) {
        Query q = new Query();
        q.addCriteria(Criteria.where("outerAppConfigId").is(outerAppConfigId));
        return mongoTemplate.find(q, OuterAppIpConfig.class);
    }

    @Override
    public boolean exist(String outerAppConfigId, String ip,String domain) {
        Query q = new Query();
        q.addCriteria(Criteria.where("outerAppConfigId").is(outerAppConfigId));
        q.addCriteria(Criteria.where("ip").is(ip).and("domain").is(domain));
        OuterAppIpConfig o = mongoTemplate.findOne(q, OuterAppIpConfig.class);
        if(Objects.isNull(o)){
            return false;
        }
        return true;
    }
}
